HackerRank Maximum Palindromes
提出
code: python
import math
import os
import random
import re
import sys
#
# Complete the 'initialize' function below.
#
# The function accepts STRING s as parameter.
#
def initialize(s):
# This function is called once before all queries.
print(s)
#
# Complete the 'answerQuery' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. INTEGER l
# 2. INTEGER r
#
def answerQuery(l, r, s):
# Return the answer for this query modulo 1000000007.
for i in range(len(s)):
alp[ord(si) - ord('a')] += 1 not_zero_alp = list(filter(lambda x: x != 0, alp))
# week ee abab aaaabcd aaaabbbbcd
print(not_zero_alp)
if __name__ == '__main__':
s = input()
q = int(input().strip())
for q_itr in range(q):
first_multiple_input = input().rstrip().split()
l = int(first_multiple_input0) r = int(first_multiple_input1) result = answerQuery(l, r, s)
fptr.write(str(result) + '\n')
fptr.close()
解答
code: python
import math
import os
import random
import re
import sys
from collections import Counter, defaultdict
from math import factorial
fact = dict()
powr = dict()
dist = defaultdict(lambda : Counter(""))
m = 10 ** 9 + 7
def initialize(s):
fact0, powr0, dist0 = 1, 1, Counter(s0) for j in range(1, len(s)):
distj = distj-1 + Counter(sj) def power(x, n, m):
if n == 1:
return x % m
elif n % 2 == 0:
return power(x ** 2 % m, n // 2, m)
else:
return (x * power(x ** 2 % m, (n - 1) // 2, m)) % m
def answerQuery(s, l, r):
# Return the answer for this query modulo 1000000007.
p, count, value = 0, 0, 1
for c in b.values():
if c >= 2:
count += c // 2
value = (value * factc // 2) % m if c % 2 == 1:
p += 1
return (max(1, p) * factcount * power(value, m - 2, m)) % m if __name__ == '__main__':
s = input()
initialize(s)
q = int(input().strip())
for q_itr in range(q):
first_multiple_input = input().rstrip().split()
l = int(first_multiple_input0) r = int(first_multiple_input1) result = answerQuery(s,l, r)
fptr.write(str(result) + '\n')
fptr.close()
メモ